<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var array = [55, 12, 63, 42, 2, 34];
        function bubbleSort(arr) {
            for (var j = 1; j < arr.length; j++) {
                for (var i = 0; i < arr.length - j; i++) {
                    if (arr[i] > arr[i + 1]) {
                        //交换位置
                        var temp = arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = temp
                    }
                }
            }
        }
        bubbleSort(array);
        console.log(array);
    </script>
</body>

</html>
<!-- for (var i = 1; i < arr.length; i++) {
	for (var j = 0; j < arr.length - i; j++) {
		// 经过这样for循环,我们能找到本轮最大的数,并排在本轮尾部
		if (arr[j] > arr[j + 1]) {
			// 交换位置
			var temp = arr[j];
			arr[j] = arr[j + 1];
			arr[j + 1] = temp;
		}
	}
}
console.log(arr); // [1, 5, 12, 32, 40, 41] -->